home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4570 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  77 lines

  1. Path: gail.ripco.com!mambuhl
  2. From: mambuhl@ripco.com (Martin Ambuhl)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Command line arg and
  5. Date: 5 Feb 1996 18:38:50 GMT
  6. Organization: Ripco Communications, Inc.
  7. Message-ID: <4f5irq$44v@gail.ripco.com>
  8. NNTP-Posting-Host: golden.ripco.com
  9.  
  10. Chris Frenck <cfrenck@soleil.acomp.usf.edu>
  11. in <311273B8.25BF@soleil.acomp.usf.edu> asks:
  12.  
  13. >I am a beginning C student and have run into a bit of a problem. When compiling
  14. >on GCC
  15. >(required) I get a segmentation fault. If I compile with MSVC++ at home it runs
  16. >fine.
  17. >Could someone please tell me what the error is?
  18.  
  19.  
  20. First, here's something else to fix in check():
  21.  
  22. #include <stdlib.h>             /* mha - added */
  23. /*....*/
  24.     if (argc > 1) {
  25.         for (count = 1; count < argc; count++); /* counts arguments */
  26.     } else {                    /* mha - added */
  27.         printf("No command line arguments to process.");
  28.         exit(EXIT_FAILURE);     /* mha - added */
  29.     }                           /* mha - added */
  30.  
  31. The segfault you saw does not actually occur in palin(), but in
  32. reverse().  Check the following and see if it helps you.  It will make
  33. the segfault go away, but both check() and reverse() (and palin() and
  34. forward()) could stand more work.  One reason is that `count' is
  35. actually useless. All the information `count' contains is also contained
  36. in `argc'.
  37.  
  38. void reverse(int count, char *argv[])
  39. {
  40.     /* Variable Declarations */
  41.     /* mha - removed "char *argument;" */
  42.     /* mha - removed "int tempctr;" (see "for" comment) */
  43.     char *revphrase;
  44.  
  45.     /* Function Prototype */
  46.     char palin(char[], char[]);
  47.  
  48.     /* mha - note that tempctr has no function at all in
  49.      * "for (tempctr = count; count > 1; count--) {", etc.
  50.      * replaced for pedagogical reasons with the following.  Note that
  51.      * without the initial count--, the first argv[count] is a null
  52.      * pointer (in "prog backward foobar", count = 3 and "foobar" is
  53.      * actually argv[2]) */
  54.  
  55.     for (count--; count > 1; count--) {
  56.         /* mha - removed "argument = argv[count];"); */
  57.  
  58.         revphrase = malloc(strlen(argv[count]) + 1);
  59.         /* mha - the above added; without this no space has been
  60.          * allocated for revphrase */
  61.  
  62.         palin(argv[count], revphrase);
  63.         /* mha - the above was "palin(argument, revphrase);" */
  64.  
  65.         printf("%s ", revphrase);
  66.  
  67.         /* mha - removed "tempctr++;" since not relevant (see "for"
  68.          * comment) */
  69.  
  70.         free(revphrase);        /* mha - added */
  71.     }
  72. }
  73.                                                                       
  74. --
  75. * Martin Ambuhl       net: mambuhl@ripco.com
  76. * Chicago, IL (USA)    
  77.